package com.zq.assistant.func.apifactory;

import com.zq.assistant.func.apifactory.invocationhandler.HttpInvocationHandler;
import com.zq.assistant.func.apifactory.handler.OKHttpRequestHandler;

import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;

/**网络接口工厂
 * Created by zhangqiang on 2016/5/18.
 */
public class   APIFactory {

    private static final Map<Class,Object> proxyCache = new HashMap<>();

    public static <T> T get(Class<T> clazz){

        T obj = (T) proxyCache.get(clazz);
        if(obj != null){
            return obj;
        }

        HttpInvocationHandler invocationHandler = new HttpInvocationHandler(new OKHttpRequestHandler());

        obj = (T) Proxy.newProxyInstance(clazz.getClassLoader(),new Class[]{clazz},invocationHandler);

        if(obj != null){

            proxyCache.put(clazz,obj);
        }
        return obj;
    }
}
